package com.zbw.common.controller;


import com.zbw.common.ask.AskBookComment;
import com.zbw.common.cache.CacheKey;
import com.zbw.common.cache.CacheService;
import com.zbw.common.controller.Feign.BookFeign;
import com.zbw.common.entity.Book;
import com.zbw.common.entity.BookCategory;
import com.zbw.common.entity.BookComment;
import com.zbw.common.entity.BookIndex;
import com.zbw.common.enums.ResponseStatus;
import com.zbw.common.service.BookService;
import com.zbw.common.service.IndexCacheService;
import com.zbw.common.vo.BookCommentVO;
import com.zbw.common.vo.BookSpVO;
import com.zbw.common.vo.BookVo;
import com.zbw.common.vo.UserDetails;
import io.github.xxyopen.model.page.PageBean;
import io.github.xxyopen.model.page.builder.pagehelper.PageBuilder;
import io.github.xxyopen.model.resp.RestResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
@Slf4j
public class BookController extends BaseController{

    private final IndexCacheService indexCacheService;

    private final BookFeign bookFeign;

    private final BookService bookService;

    private final CacheService cacheService;
    //首页点击数据
    @GetMapping("listClickRank")
    public RestResult<List<Book>> listClickRank() {
        return RestResult.ok(indexCacheService.listClickRank());
    }

    /**
     * 查询首页新书榜单数据
     */
    @GetMapping("listNewRank")
    public RestResult<List<Book>> listNewRank() {
        return RestResult.ok(indexCacheService.listNewRank());
    }

    /**
     * 查询首页更新榜单数据
     */
    @GetMapping("listUpdateRank")
    public RestResult<List<BookVo>> listUpdateRank() {
        return RestResult.ok(indexCacheService.listUpdateRank());
    }
    //增加访问量
    @PostMapping("addVisitCount")
    public  RestResult<Void> addVistCount(Long bookId){
        bookFeign.addcount(bookId);
        return RestResult.ok();
    };
    //分页查询评论
    @GetMapping("listCommentByPage")
    public RestResult<PageBean<BookCommentVO>> listCommentByPage(@RequestParam("bookId") Long bookId,
                                                     @RequestParam(value = "curr", defaultValue = "1") int page,
                                                     @RequestParam(value = "limit", defaultValue = "5") int pageSize){
        AskBookComment askBook= new AskBookComment();
        askBook.setBookId(bookId);
        askBook.setNum(page);
        askBook.setSize(pageSize);
        return RestResult.ok(bookService.selectComment(askBook));
    }
    //排行榜

    @GetMapping("listRank")
    public RestResult<List<Book>> listRank(@RequestParam(value = "type", defaultValue = "0") Byte type,
                                           @RequestParam(value = "limit", defaultValue = "30") Integer limit) {
        List<Book> books = new ArrayList<>(1);
        switch (type){
            case 0:

                 books =(List<Book>) cacheService.getObject(CacheKey.Book_Rank_Key_0);
                if (books == null || books.size() == 0){
                    books = bookFeign.listRank(type, limit);
                    cacheService.setObject(CacheKey.Book_Rank_Key_0,books,600);
                }
                return RestResult.ok(books);

            case 1:

                 books =(List<Book>) cacheService.getObject(CacheKey.Book_Rank_Key_1);
                if (books == null || books.size() == 0){
                    books = bookFeign.listRank(type, limit);
                    cacheService.setObject(CacheKey.Book_Rank_Key_1,books,600);
                }
                return RestResult.ok(books);

            case 2:

                 books =(List<Book>) cacheService.getObject(CacheKey.Book_Rank_Key_2);
                if (books == null || books.size() == 0){
                    books = bookFeign.listRank(type, limit);
                    cacheService.setObject(CacheKey.Book_Rank_Key_2,books,600);
                }
                return RestResult.ok(books);

            case 3:

                books =(List<Book>) cacheService.getObject(CacheKey.Book_Rank_Key_3);
                if (books == null || books.size() == 0){
                    books = bookFeign.listRank(type, limit);
                    cacheService.setObject(CacheKey.Book_Rank_Key_3,books,600);
                }
                return RestResult.ok(books);

            default:

                break;
        }

        return null;
    }
    /**
     * 分页搜索
     */
    @GetMapping("searchByPage")
    public RestResult<?> searchByPage(BookSpVO bookSP, @RequestParam(value = "curr", defaultValue = "1") int page,
                                      @RequestParam(value = "limit", defaultValue = "20") int pageSize) {
        return RestResult.ok(bookService.searchByPage(bookSP, page, pageSize));
    }
    /**
     *
     * 查询书本分类
     */
    @GetMapping("listBookCategory")
    public RestResult<List<BookCategory>> listBookCategory() {
        return RestResult.ok(bookFeign.listBookCategory());
    }

    @GetMapping("queryBookIndexAbout")
    public RestResult<Map<String,Object>> queryBookIndexAbout(Long bookId , Long lastBookIndexId){
        Map<String, Object> data = new HashMap<>(2);
        data.put("bookIndexCount", bookFeign.queryIndexCount(bookId));
        String lastBookContent = bookFeign.queryBookContent(bookId, lastBookIndexId);
        data.put("lastBookContent", lastBookContent);
        return RestResult.ok(data);
    }

    /**
     * 目录页
     */
    @GetMapping("/queryIndexList")
    public RestResult<PageBean<BookIndex>> indexList(Long bookId,
                                                     @RequestParam(value = "curr", defaultValue = "1") int page,
                                                     @RequestParam(value = "limit", defaultValue = "5") int pageSize,
                                                     @RequestParam(value = "orderBy", defaultValue = "index_num desc") String orderBy) {
        return RestResult.ok(PageBuilder.build(bookFeign.queryIndexList(bookId, orderBy, page, pageSize)));
    }


    /**
     * 新增评价
     */
    @PostMapping("addBookComment")
    public RestResult<?> addBookComment(BookComment comment, HttpServletRequest request) {
        UserDetails userDetails = getUserDetails(request);
        if (userDetails == null) {
            return RestResult.fail(ResponseStatus.NO_LOGIN);
        }
        bookFeign.addBookComment(userDetails.getId(), comment);
        return RestResult.ok();
    }
}
